package com.xywy.me.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.util.DialogUtils;
import com.xywy.me.bean.Jtcy;

import java.util.List;

/**
 * 名称: 我 - 家庭成员
 * 描述:
 *
 * 类型: JAVA
 * 最近修改时间:${date} ${time}
 * @since  2014/8/8
 * @author xgd
 */
public class JtcyAdapter extends BaseAdapter {

    private Context mContext;
    private List<Jtcy> list;
    private Dialog  dlg;
    public JtcyAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void setData(List<Jtcy>  jtcy){
        this.list = jtcy;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hold hold;
        if(convertView==null){
//            convertView = LayoutInflater.from(mContext).inflate(R.layout.me_jtcy_item, null);

            hold = new Hold();
            hold.ll = (LinearLayout) convertView.findViewById(R.id.ll_item_me_jtcy);
//            hold.check = (ImageView) convertView.findViewById(R.id.iv_me_jtcy_duihao);
//            hold.name = (TextView) convertView.findViewById(R.id.tv_me_jtcy_name);
//            hold.mainAc = (ImageView) convertView.findViewById(R.id.iv_me_jtcy_ifmain);
//            hold.sex = (TextView) convertView.findViewById(R.id.tv_me_jtcy_sex);
//            hold.birth = (TextView) convertView.findViewById(R.id.tv_me_jtcy_age);
//
//            hold.time = (TextView) convertView.findViewById(R.id.tv_me_jtcy_time);
//            hold.height = (TextView) convertView.findViewById(R.id.tv_me_jtcy_height);

            convertView.setTag(hold);
        }else{
            hold = (Hold) convertView.getTag();
        }
        final Jtcy  jtcy=list.get(position);

        //控件赋值
         hold.name.setText(jtcy.getName());
         hold.sex.setText(jtcy.getSex());
         hold.height.setText(jtcy.getHeight()+"cm");
       /*  hold.birth.setText(jtcy.getBirth());
         hold.time.setText(jtcy.getTime());
         hold.weight.setText(jtcy.getWeight());

             if(jtcy.getCheckEd()){

        }else{
            hold.ll.setBackgroundColor(mContext.getResources().getColor(R.color.find_bg));
        }
       */
        if(jtcy.getAccountid()==0f){
            hold.mainAc.setVisibility(View.VISIBLE);
            hold.check.setVisibility(View.VISIBLE);
            hold.ll.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        }else{
            hold.ll.setBackgroundColor(mContext.getResources().getColor(R.color.me_jtcy_list_item));
        }

         //事件
//         hold.ll.setOnClickListener(new View.OnClickListener() {
//             @Override
//             public void onClick(View view) {
//                 Intent intent =new Intent(mContext, JtcyDetailActivity.class);
//                   intent.putExtra("jtcy",jtcy);
//                  mContext.startActivity(intent);
//             }
//         });
        //长按事件
        hold.ll.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //弹提示框
                Bundle bundle=new Bundle();
                bundle.putString("title",mContext.getString(R.string.me_jtcy_dialog_delete));
                dlg = DialogUtils.creatDialog(mContext, 1, bundle, itemsOnClick);

                return true;
            }
        });
        return convertView;
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener(){
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dialog_button_ok:
                    dlg.dismiss();

                    break;
                case R.id.dialog_button_cancel:
                    dlg.dismiss();
                    break;
                default:
                    break;
            }


        }

    };

    public class Hold{
          LinearLayout ll;
          ImageView check;     //是否是当前用户
          TextView  name;      //名字
          TextView  sex;       //性别
          TextView  height;    //身高
          TextView  birth;     //生日
          TextView  time;      //添加时间
          ImageView mainAc ;   //是否主账号
    }

}
